import { Scene_Base } from "@/scenes/Scene_Base.js";
import { AudioManager } from "@/managers/AudioManager.js";
import { ImageManager } from "@/managers/ImageManager.js";
import { Sprite } from "@/sprites/Sprite.js";
import { Input } from "@/core/Input.js";
import { TouchInput } from "@/core/TouchInput.js";
import { SceneManager } from "@/managers/SceneManager.js";
import { Scene_Title } from "@/scenes/Scene_Title.js";

export class Scene_Gameover extends Scene_Base {
    constructor() {
        super();
    }

    create() {
        super.create();
        this.playGameoverMusic();
        this.createBackground();
    }

    start() {
        super.start();
        this.startFadeIn(this.slowFadeSpeed(), false);
    }

    update() {
        if (this.isActive() && !this.isBusy() && this.isTriggered()) {
            this.gotoTitle();
        }
        super.update();
    }

    stop() {
        super.stop();
        this.fadeOutAll();
    }

    terminate() {
        super.terminate();
        AudioManager.stopAll();
    }

    playGameoverMusic() {
        AudioManager.stopBgm();
        AudioManager.stopBgs();
        AudioManager.playMe($dataSystem.gameoverMe);
    }

    createBackground() {
        this._backSprite = new Sprite();
        this.addChild(this._backSprite);
        ImageManager.loadSystem('GameOver').then(source=>{
            this._backSprite.source=source;
        });
    }

    isTriggered() {
        return Input.isTriggered('ok') || TouchInput.isTriggered();
    }

    gotoTitle() {
        SceneManager.goto(Scene_Title);
    }
}